<?php
	function copyAsProductImage($source, $pid, $filename, $vid=0, $type, $thumb)
	{
		if ((is_file($source)) && ($filename != ""))
		{
			$info = pathinfo($source);
			$temp_name = $filename;
			$extension = $info['extension'];
			$filename = md5(uniqid());
			$new_name = $filename.'.'.$extension;
			
			copy($source, PRODUCT_PATH.$new_name);
			
			resizePhoto(PRODUCT_PATH.$new_name, $type, $thumb['PRODUCT'][0]['MAXW'], $thumb['PRODUCT'][0]['MAXH']);
			$daName = $new_name;
			$i=0;
			$j=0;
			foreach($thumb['PRODUCT'] as $thumb)
			{
				if($j > 0)
				{
					if($i == 0)
					{ 
						$new_name = $daName; 
						$i = 1;
					}
					else
					{ $new_name = $thumb['MAXW'].'_'.$daName; }
					
					if(is_file(PRODUCT_PATH.$daName))
					{
						if (copy(PRODUCT_PATH.$daName, PRODUCT_PATH.'thumbs/'.$new_name)) 
						{
							if($thumb['RESIZING_TYPE'] == 1)
							{ makeIcons_MergeCenter(PRODUCT_PATH.'thumbs/'.$new_name, PRODUCT_PATH.'thumbs/'.$new_name, $thumb['MAXW'], $thumb['MAXH']); }
							else
							{ resizePhoto(PRODUCT_PATH.'thumbs/'.$new_name, $type, $thumb['MAXW'], $thumb['MAXH']); }
						}
					}
				}
				$j++;
			}
			
			if(is_file(PRODUCT_PATH.$daName))
			{
				$default = 1;
				$select = 'SELECT id FROM '.DB_PERFIX.'shop_images WHERE product_id='.$pid.' LIMIT 1;';
				$result = mysql_query($select);
				if(mysql_num_rows($result) > 0)
				{ 
					while($row = mysql_fetch_array($result))
					{
						if($row['id'] > 0)
						{ $default = 0; }
					}
				}
				
				$insert = 'INSERT INTO '.DB_PERFIX.'shop_images (`id`, `product_id`, `variation_id`, `image`, `default`) ';
				$insert .= ' VALUES (NULL, '.$pid.', '.$vid.', "'.$daName.'", '.$default.');';
				$result = mysql_query($insert);
				if(mysql_num_rows($result) > 0)
				{
					$update = 'UPDATE '.DB_PERFIX.'product SET has_image=1 WHERE id='.$pid.';';
					$result = mysql_query($update);
				}
				return $result;
			}
		}
	}
?>